package timeline;
/**

 * Copyright (c) 2008, 2012 Oracle and/or its affiliates.

 * All rights reserved. Use is subject to license terms.

 */

import javafx.application.Application;

import javafx.scene.Group;

import javafx.scene.Scene;

import javafx.stage.Stage;

import javafx.animation.*;

import javafx.event.EventHandler;

import javafx.scene.Node;

import javafx.scene.paint.Color;

import javafx.scene.shape.Rectangle;

import javafx.util.Duration;

 

/**

 * A sample in which various transitions are executed sequentially.

 *

 * @related animation/transitions/FadeTransition

 * @related animation/transitions/FillTransition

 * @related animation/transitions/ParallelTransition

 * @related animation/transitions/PathTransition

 * @related animation/transitions/PauseTransition

 * @related animation/transitions/RotateTransition

 * @related animation/transitions/ScaleTransition

 * @related animation/transitions/StrokeTransition

 * @related animation/transitions/TranslateTransition

 * @see javafx.animation.SequentialTransition

 * @see javafx.animation.Transition

 */

public class SequentialTransitionSample extends Application {

 

    private SequentialTransition sequentialTransition;

 

    private void init(Stage primaryStage) {

        Group root = new Group();

        primaryStage.setResizable(false);

        primaryStage.setScene(new Scene(root, 400,100));

        // create rectangle

        Rectangle rect = new Rectangle(-25,-25,50, 50);

        rect.setArcHeight(15);

        rect.setArcWidth(15);

        rect.setFill(Color.CRIMSON);

        rect.setTranslateX(50);

        rect.setTranslateY(50);

        root.getChildren().add(rect);

        // create 4 transitions

        FadeTransition fadeTransition =

                FadeTransitionBuilder.create()

                .duration(Duration.seconds(1))

                .fromValue(1)

                .toValue(0.3)

                .cycleCount(2)

                .autoReverse(true)

                .build();

        TranslateTransition translateTransition =

                TranslateTransitionBuilder.create()

                .duration(Duration.seconds(2))

                .fromX(50)

                .toX(375)

                .cycleCount(2)

                .autoReverse(true)

                .build();       

        RotateTransition rotateTransition = 

                RotateTransitionBuilder.create()

                .duration(Duration.seconds(2))

                .byAngle(180)

                .cycleCount(4)

                .autoReverse(true)

                .build();

        ScaleTransition scaleTransition =

                ScaleTransitionBuilder.create()

                .duration(Duration.seconds(2))

                .toX(2)

                .toY(2)

                .cycleCount(2)

                .autoReverse(true)

                .build();

        // create sequential transition to do 4 transitions one after another       

        sequentialTransition = SequentialTransitionBuilder.create()

                .node(rect)

                .children(fadeTransition, translateTransition, rotateTransition,

                           scaleTransition)

                .cycleCount(Timeline.INDEFINITE)

                .autoReverse(true)

                .build();

    }

 

    public void play() {

        sequentialTransition.play();

    }

 

    @Override public void stop() {

        sequentialTransition.stop();

    }

 

    public double getSampleWidth() { return 400; }

 

    public double getSampleHeight() { return 100; }

 

    @Override public void start(Stage primaryStage) throws Exception {

        init(primaryStage);

        primaryStage.show();

        play();

    }

    public static void main(String[] args) { launch(args); }

}